#include <REGX52.H>
#include"LCD1602.h"
#include"DS1302.h"
#include"key.h"
#include"delay.h"
#include"timer0.h"
#include"buffer.h"
#include"AT24C02.H"
#include"DS18B20.H"

unsigned char keynum,MODE,place,flashsign;
unsigned char min=30;
unsigned char hour=12;
float T;
void setLCD()
{
	if(keynum==2)
	{
		place++;
		place%=6;
		
	}
	if(keynum==3)
	{
		DS1302_TIME[place]++;
		if(DS1302_TIME[0]>=100)
		{
			DS1302_TIME[0]=0;
		}
		if(DS1302_TIME[1]>=13)
		{
			DS1302_TIME[1]=1;
		}
		if(DS1302_TIME[1]==1||DS1302_TIME[1]==3||DS1302_TIME[1]==5||DS1302_TIME[1]==7||DS1302_TIME[1]==8||DS1302_TIME[1]==10||DS1302_TIME[1]==12)
		{
			if(DS1302_TIME[2]>=32){DS1302_TIME[2]=1;}		
		}
		if(DS1302_TIME[1]==4||DS1302_TIME[1]==6||DS1302_TIME[1]==9||DS1302_TIME[1]==11)
		{
			if(DS1302_TIME[2]>=31){	DS1302_TIME[2]=1;}	
		}
		if(DS1302_TIME[1]==2)
		{
			if(DS1302_TIME[0]%4==0)
			{
				if(DS1302_TIME[2]>=29){DS1302_TIME[2]=1;}
			}
			if(DS1302_TIME[0]%4!=0)
			{
				if(DS1302_TIME[2]>=30){DS1302_TIME[2]=1;}
			}
		}
		if(DS1302_TIME[3]>=24){DS1302_TIME[3]=0;}
		if(DS1302_TIME[4]>=60){DS1302_TIME[4]=0;}
		if(DS1302_TIME[5]>=60){DS1302_TIME[5]=0;}
	}
	if(keynum==4)
	{ 
		DS1302_TIME[place]--;
		if(DS1302_TIME[0]<0){DS1302_TIME[0]=99;}
		if(DS1302_TIME[1]<0){DS1302_TIME[1]=12;}
		if(DS1302_TIME[1]==1||DS1302_TIME[1]==3||DS1302_TIME[1]==5||DS1302_TIME[1]==7||DS1302_TIME[1]==8||DS1302_TIME[1]==10||DS1302_TIME[1]==12)
		{
			if(DS1302_TIME[2]<0){DS1302_TIME[2]=31;}
			if(DS1302_TIME[2]>=32){DS1302_TIME[2]=1;}
		}
		if(DS1302_TIME[1]==4||DS1302_TIME[1]==6||DS1302_TIME[1]==9||DS1302_TIME[1]==11)
		{
			if(DS1302_TIME[2]<0){DS1302_TIME[2]=30;}
			if(DS1302_TIME[2]>=31){	DS1302_TIME[2]=1;}	
		}
		if(DS1302_TIME[1]==2)
		{
			if(DS1302_TIME[0]%4==0)
			{
				if(DS1302_TIME[2]<0){DS1302_TIME[2]=28;}
				if(DS1302_TIME[2]>=29){DS1302_TIME[2]=1;}
				
			}
			if(DS1302_TIME[0]%4!=0)
			{
				if(DS1302_TIME[2]<0){DS1302_TIME[2]=29;}
				if(DS1302_TIME[2]>=30){DS1302_TIME[2]=1;}
			}
		}
		if(DS1302_TIME[3]<0)
		{
			DS1302_TIME[3]=23;
		}
		if(DS1302_TIME[4]<0)
		{
			DS1302_TIME[4]=59;
		}
		if(DS1302_TIME[5]<0)
		{
			DS1302_TIME[5]=59;
		}
	}
	
	if(place==0&&flashsign==1){LCD_ShowString(1,1,"  ");}
	else{LCD_ShowNum(1,1,DS1302_TIME[0],2);}
	if(place==1&&flashsign==1){LCD_ShowString(1,4,"  ");}
	else{LCD_ShowNum(1,4,DS1302_TIME[1],2);}
	if(place==2&&flashsign==1){LCD_ShowString(1,7,"  ");}
	else{LCD_ShowNum(1,7,DS1302_TIME[2],2);}
	if(place==3&&flashsign==1){LCD_ShowString(2,1,"  ");}
	else{LCD_ShowNum(2,1,DS1302_TIME[3],2);}
	if(place==4&&flashsign==1){LCD_ShowString(2,4,"  ");}
	else{LCD_ShowNum(2,4,DS1302_TIME[4],2);}
	if(place==5&&flashsign==1){LCD_ShowString(2,7,"  ");}
	else{LCD_ShowNum(2,7,DS1302_TIME[5],2);}
	
//	LCD_ShowNum(1,1,DS1302_TIME[0],2);
//	LCD_ShowNum(1,4,DS1302_TIME[1],2);
//	LCD_ShowNum(1,7,DS1302_TIME[2],2);
//	LCD_ShowNum(2,1,DS1302_TIME[3],2);
//	LCD_ShowNum(2,4,DS1302_TIME[4],2);
//	LCD_ShowNum(2,7,DS1302_TIME[5],2);
//	LCD_ShowNum(2,10,place,2);
//	LCD_ShowNum(2,13,flashsign,2);
}
void showLCD()
{
	
	DS1302_READTIME();
	LCD_ShowNum(1,1,DS1302_TIME[0],2);
	LCD_ShowNum(1,4,DS1302_TIME[1],2);
	LCD_ShowNum(1,7,DS1302_TIME[2],2);
	LCD_ShowNum(2,1,DS1302_TIME[3],2);
	LCD_ShowNum(2,4,DS1302_TIME[4],2);
	LCD_ShowNum(2,7,DS1302_TIME[5],2);
	if(DS1302_TIME[3]==hour&&DS1302_TIME[4]==min){buffer_int(1000);}
	if(keynum==4){void buffer_off();}
	
}
//void ds18b20_new()
//{
//	DS18B20_ConvertT();
//	Delay(1000);
//	T=DS18B20_ReadT();
//	if(T<0)
//	{
//		LCD_ShowChar(2,10,'-');
//		T=-T;
//	}
//	else
//	{
//		LCD_ShowChar(2,10,'+');
//	}
//		
//		
//	
//}
void main()
{
	
	DS18B20_ConvertT();
	Delay(1000);
	LCD_Init();
	Timer0Init();
	DS1302_INIT();
	LCD_ShowString(1,1,"  -  -  ");
	LCD_ShowString(2,1,"  :  :  ");
	LCD_ShowString(1,10,"TL:");
	DS1302_SETTIME();
 	while(1)
	{ 
		DS18B20_ConvertT();
		Delay(1000);
		
		T=DS18B20_ReadT();
		LCD_ShowNum(2,11,T,2);
		keynum=key();
		if(keynum==1)
		{
			MODE++;
			if(MODE>1){MODE=0;Delay(10);DS1302_SETTIME();}
		}
		
		switch(MODE)
		{
			
			case 1:setLCD();
					break;
			case 0:showLCD();
					break;
		}
		
	}
}

void Timer0_Routine()  interrupt 1
{
   static unsigned int count_T0=0;	
	 static unsigned int count_T1=0;
				TL0 = 0x18;		
				TH0 = 0xFC;
				count_T0++;
				count_T1++;
	if(count_T0>500)
	{
		flashsign=(!flashsign);
		count_T0=0;
	}
	if(count_T1>20){ key_loop();}
}
//void Timer1_Routine()  interrupt 3
//{
//	static unsigned int count_T3=0;
//	TL0 = 0x18;		
//	TH0 = 0xFC;
//	count_T3++;
//	if(count_T3>=20)
//	{
//		count_T3=0;
//		ds18b20_new();
//		
//		
//		
//	}
//}